package com.iflytek.http.impl;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.iflytek.http.entity.HandledResult;
import com.iflytek.http.interfaces.Handleable;
import com.iflytek.http.interfaces.OperationListener;

public class PostOperation extends AbstractOperation
{
  private List<NameValuePair> params;

  public PostOperation(int id, String url, Class<? extends Handleable> handlerClass, OperationListener listener)
  {
    super(id, url, handlerClass, listener);
    this.params = new ArrayList();
  }

  public void addBasicNameValuePairs(String key, String value) {
    this.params.add(new BasicNameValuePair(key, value));
  }

  public void addNameValuePair(NameValuePair nameValuePair) {
    this.params.add(nameValuePair);
  }

  public void addNameValuePairs(Collection<NameValuePair> nameValuePairs) {
    this.params.addAll(nameValuePairs);
  }

  public void setParamList(List<NameValuePair> paramList) {
    this.params = paramList;
  }

  protected void handleOperation()
  {
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = null;
    try {
      HttpPost postRequest = new HttpPost(this.url);
      String encoding = this.mEncoding;
      postRequest.setEntity(new UrlEncodedFormEntity(this.params, encoding));
      response = client.execute(postRequest);

      StatusLine status = response.getStatusLine();
      int code = status.getStatusCode();
      if (code != 200) {
        if (getOperationListener() != null) {
          sendNotOlayMsg(code, EntityUtils.toString(response.getEntity()));
        }
        return;
      }

      Handleable parser = (Handleable)this.mHandlerClass.newInstance();
      String content = null;
      InputStream is = null;
      if (parser.getContentType() == 1)
        content = EntityUtils.toString(response.getEntity());
      else if (parser.getContentType() == 2) {
        is = response.getEntity().getContent();
      }

      if (getOperationListener() == null)
      {
        System.out.println("activity null");
        if ((this.cacheKey != null) && (this.cacher != null)) {
          HandledResult result = parse(content, is);
          this.cacher.cache(this.cacheKey, result);
        }
      } else {
        HandledResult result = parse(content, is);
        sendSuccessMsg(result);
      }
      if (is != null)
        is.close();
    }
    catch (IOException e)
    {
      e.printStackTrace();
      sendExceptionMsg(e);
    } catch (Exception e) {
      sendExceptionMsg(e);
    }
  }
}
